package com.sanfu.etl.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;

import java.util.Map;

// *********************
// ** @Date: 2021-06-29
// ** @Author: Camemax
// ** @Description: 用于支持JSON格式转换的工具类
// *********************
public class FastJSONUtils {
    // JSON转Map，根据SQL操作类型不同而不同
    public static Map<String, String> getTargetMap(String sqlType, JSONObject targetJSON) throws Exception {
        Map<String, String> targetMap ;
        TypeReference<Map<String, String>> targetType = new TypeReference<Map<String, String>>() {
        };
        switch (sqlType){
            case "I": // INSERT
                targetMap = JSONObject.parseObject(targetJSON.getString("after"), targetType);
                break;
            case "D": // DELETE
            case "U": // UPDATE
                targetMap = JSONObject.parseObject(targetJSON.getString("before"), targetType);
                break;
            default:
                throw new Exception("数据流异常，原因-SQL操作类型错误！");
        }
        return targetMap;
    }
}
